﻿using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
public class UvChange : MonoBehaviour
{

    public int width = 73, height = 49;
    public MapProjectionType type;
    public CustomUvMapper config;
#if UNITY_EDITOR
    private void Reset()
    {
        var meshName = GetComponent<MeshFilter>().sharedMesh.name;
        int spaceInd = meshName.LastIndexOf(' ');
        int xInd = meshName.LastIndexOf('x');
        int w = 72, h = 48;
        try
        {
            w = int.Parse(meshName.Substring(spaceInd + 1, xInd - spaceInd - 1));
            h = int.Parse(meshName.Substring(xInd + 1, meshName.Length - xInd - 1));
        }
        catch { }
        this.width = w + 1;
        this.height = h + 1;
        config = UnityEditor.AssetDatabase.LoadAssetAtPath<CustomUvMapper>("Assets/UVConverters/default converter.asset");
    }
#endif

    [ContextMenu("执行 uv变换")]
    void Start()
    {
        var mesh = GetComponent<MeshFilter>().mesh;
        var uv = mesh.uv;



        for (int v = 0; v < height; v++)
            for (int h = 0; h < width; h++)
            {
                var origin = new Vector2(1 - h / (width - 1f), 1 - v / (height - 1f));
                Vector2 result = origin;
                if (type == MapProjectionType.等差分纬线多圆锥)
                {
                    result = UvMapTool.CustomProjection(origin, config);
                }
                else if (type == MapProjectionType.伪墨卡托投影)
                {
                    var lat = (origin.y - 0.5f) * Mathf.PI;
                    result.y = UvMapTool.PseudoMercatorProjection(lat);

                }
                uv[v * width + h] = result;
            }
        mesh.uv = uv;
    }


}

#if UNITY_EDITOR

[UnityEditor.CustomEditor(typeof(UvChange))]
[UnityEditor.CanEditMultipleObjects]
class UvChangeEditor : UnityEditor.Editor
{
    UnityEditor.SerializedProperty
        widthProp, heightProp,
        typeProp,
        configProp;

    private void OnEnable()
    {
        widthProp = serializedObject.FindProperty("width");
        heightProp = serializedObject.FindProperty("height"); 
         typeProp = serializedObject.FindProperty("type");
        configProp = serializedObject.FindProperty("config");
        
    }

    public override void OnInspectorGUI()
    {

        UnityEditor.EditorGUILayout.BeginHorizontal();
        var temp = UnityEditor.EditorGUIUtility.labelWidth;
        UnityEditor.EditorGUIUtility.labelWidth = UnityEditor.EditorGUIUtility.singleLineHeight * 6;
        UnityEditor.EditorGUILayout.PropertyField(widthProp);

        UnityEditor.EditorGUILayout.PropertyField(heightProp);
        UnityEditor.EditorGUIUtility.labelWidth = temp;
        UnityEditor.EditorGUILayout.EndHorizontal();

        typeProp.enumValueIndex = (int)(MapProjectionType)
            UnityEditor.EditorGUILayout.EnumPopup("投影模式", (MapProjectionType)typeProp.enumValueIndex);
         
        if (typeProp.intValue == (int)MapProjectionType.等差分纬线多圆锥)
        {
            UnityEditor.EditorGUILayout.PropertyField(configProp);
        }
        serializedObject.ApplyModifiedProperties();
    }
}

#endif